package com.course.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.course.entity.Admin;
import com.course.repository.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class LoginHandler {

    @Autowired
    private AdminRepository adminRepository;

    @PostMapping("/login")
    public String login(@RequestBody String resq) {
        JSONObject jsonObject = JSON.parseObject(resq);
        String account = jsonObject.getString("account");
        String password = jsonObject.getString("password");

        Admin admin = adminRepository.findAdminByAccountAndPassword(account, password);
        if (admin != null) {
            return "success";
        } else {
            return "error";
        }
    }
}
